//
//  ViewController.swift
//  AlamofireEncapsulate
//
//  Created by 李煜 on 2024/4/18.
// https://www.baidu.com/s?ie=UTF-8&wd=alamofire%E5%B0%81%E8%A3%85
// https://www.jianshu.com/p/af9ffbcd734b

import Alamofire
import UIKit
import CWLog

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

//        noParamNoRes()
//        paramNoRes()
//        paramRes()
//        responseHandler()
//        responseDataHandler()
//        responseStringHandler()
//        responseJsonHandler()
        test()
    }

    func noParamNoRes() {
        let data = AF.request("https://httpbin.org/get")
        print(data)
    }

    func paramNoRes() {
        let data = AF.request("https://httpbin.org/get", parameters: ["foo": "bar"])
        print(data)
    }

    func paramRes() {
        let data = AF.request("https://httpbin.org/get", parameters: ["foo": "bar"])
            .responseJSON { res in
                print(res.result)
                print(res.response)
                print("data: \n \(res.data)")
                print("res.result: \n \(res.result)")
                if let json = res.value {
                    print("序列化json: \n\(json)")
                }
            }
            
        print(data)
    }

    func test() {
        let params = ["appid": "__APP__ANDROID",
                      "versionCode": "10000"]
        AF.request("https://hhr.zhongnongyigou.com/znHhr/common/getAppDownloadAdd", parameters: params).responseJSON { res in
            guard let value = res.value else {
                return
            }
            debugPrint("test: \n",value)
//            CWLog("test: \n \(value)")
        }
    
    }
     
    func requestTest() {
//        AF.request(<#T##convertible: any URLConvertible##any URLConvertible#>, method: <#T##HTTPMethod#>, parameters: <#T##Encodable?#>, encoder: <#T##any ParameterEncoder#>, headers: <#T##HTTPHeaders?#>, interceptor: <#T##(any RequestInterceptor)?#>, requestModifier: <#T##Session.RequestModifier?##Session.RequestModifier?##(inout URLRequest) throws -> Void#>)
//        AF.request(<#T##convertible: any URLConvertible##any URLConvertible#>, method: <#T##HTTPMethod#>, parameters: <#T##Parameters?#>, encoding: <#T##any ParameterEncoding#>, headers: <#T##HTTPHeaders?#>, interceptor: <#T##(any RequestInterceptor)?#>, requestModifier: <#T##Session.RequestModifier?##Session.RequestModifier?##(inout URLRequest) throws -> Void#>)
//        AF.request(<#T##convertible: any URLConvertible##any URLConvertible#>, method: <#T##HTTPMethod#>, parameters: <#T##Parameters?#>, encoding: <#T##any ParameterEncoding#>, headers: <#T##HTTPHeaders?#>, interceptor: <#T##(any RequestInterceptor)?#>, requestModifier: <#T##Session.RequestModifier?##Session.RequestModifier?##(inout URLRequest) throws -> Void#>)
        
    }

}

// 响应处理方法
extension ViewController {
    func responseHandler() {
        AF.request("https://httpbin.org/get", parameters: ["foo": "bar"])
            .response { response in
                print("request: \(response.request)")
                print("response: \(response.response)")
                print("error: \(response.error)")

                if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
                    print("Data: \(utf8Text)")
                }
            }
    }

    func responseDataHandler() {
        AF.request("https://httpbin.org/get", parameters: ["foo": "bar"])
            .responseData { response in
                debugPrint("response: \(response)")

                if let data = response.value, let utf8Text = String(data: data, encoding: .utf8) {
                    print("Data: \(utf8Text)")
                }
            }
    }

    func responseStringHandler() {
        AF.request("https://httpbin.org/get", parameters: ["foo": "bar"])
            .responseString { response in
                guard let value = response.value else {
                    return
                }
                print("responseString Success value \n:\(value)")
            }
    }
    func responseJsonHandler() {
        AF.request("https://httpbin.org/get", parameters: ["foo": "bar"])
            .responseJSON { response in
                guard let value = response.value else {
                    return
                }
                print("responseJSON Success value \n:\(value)")
            }
    }
}
